package com.xunshangwang.pinhaohuo.ui.activities;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.ui.fragments.OrderListFragment;

/**
 * Created by xzp on 16/3/10.
 */
public class OrderListActivity extends BaseActivity{
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private OrderAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        mTabLayout= (TabLayout) this.findViewById(R.id.order_tab_layout);
        mViewPager= (ViewPager) this.findViewById(R.id.order_pager);
        mViewPager.setOffscreenPageLimit(4);
        mAdapter=new OrderAdapter(this.getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        int position=getIntent().getIntExtra("position",0);
        mViewPager.setCurrentItem(position,false);
    }
    private class OrderAdapter extends FragmentPagerAdapter{
        private String[] mOrderFunctionArray;

        public OrderAdapter(FragmentManager fm) {
            super(fm);
            mOrderFunctionArray=getResources().getStringArray(R.array.order_function_list);
        }

        @Override
        public Fragment getItem(int position) {
 //           return OrderListFragment.newInstance(position-1);
            switch (position){
                case 1:
                    return OrderListFragment.newInstance(0);
                case 2:
                    return OrderListFragment.newInstance(2);
                case 3:
                    return OrderListFragment.newInstance(1);
                default:
                    return OrderListFragment.newInstance(-1);
            }
        }

        @Override
        public int getCount() {
            return mOrderFunctionArray.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mOrderFunctionArray[position];
        }
    }
}
